//
//  FabricTableViewController.swift
//  Yidingzhi
//
//  Created by flybywind on 15/11/26.
//  Copyright © 2015年 flybywind. All rights reserved.
//

import UIKit

class FabricTableViewController: UITableViewController {
    // MARK: properties
    var receiveData : SuitStatus?
    var contents: [FabricInfo]?
    // MARK: constant
    var fabricMap = [
        "xizhuang":[
            FabricInfo(Fid: "13192", FabricBrand: "Holland&Sherry", FabricMaterial: "100%羊毛", FabricPrice: 2000, FabricImage: "xifu_f1.1", FabricScale: 2),
            FabricInfo(Fid: "13193", FabricBrand: "Holland&Sherry", FabricMaterial: "90%羊毛 10%羊绒", FabricPrice: 5000, FabricImage: "xifu_f2", FabricScale: 6),
            FabricInfo(Fid: "13194", FabricBrand: "Holland&Sherry", FabricMaterial: "100%羊毛", FabricPrice: 2000, FabricImage: "xifu_f3", FabricScale: 4),
            FabricInfo(Fid: "703731", FabricBrand: "世佳宝", FabricMaterial: "90%羊毛 10%羊绒", FabricPrice: 5000, FabricImage: "xifu_f4", FabricScale: 4),
            FabricInfo(Fid: "703732", FabricBrand: "世佳宝", FabricMaterial: "100%羊毛", FabricPrice: 5000, FabricImage: "xifu_f5", FabricScale: 4),
            FabricInfo(Fid: "703733", FabricBrand: "世佳宝", FabricMaterial: "90%羊毛 10%羊绒", FabricPrice: 5000, FabricImage: "xifu_f6", FabricScale: 4),
        ],
        "chenshan":[
            FabricInfo(Fid: "123-56 2", FabricBrand: "1857", FabricMaterial: "100%棉", FabricPrice: 550, FabricImage: "chenshan_1", FabricScale: 4),
            FabricInfo(Fid: "123-56 4", FabricBrand: "1857", FabricMaterial: "100%棉", FabricPrice: 1550, FabricImage: "chenshan_2", FabricScale: 4),
            FabricInfo(Fid: "123-56 6", FabricBrand: "鲁泰", FabricMaterial: "100%棉", FabricPrice: 550, FabricImage: "chenshan_3", FabricScale: 4),
            FabricInfo(Fid: "123-56 8", FabricBrand: "鲁泰", FabricMaterial: "100%棉", FabricPrice: 1550, FabricImage: "chenshan_4", FabricScale: 4),
            FabricInfo(Fid: "123-56 6", FabricBrand: "鲁泰", FabricMaterial: "100%棉", FabricPrice: 550, FabricImage: "chenshan_5", FabricScale: 8),
        ],
        "lingdai":[
            FabricInfo(Fid: "abc/de 100", FabricBrand: "世佳宝", FabricMaterial: "100%棉", FabricPrice: 2000, FabricImage: "tie1", FabricScale: 12),
            FabricInfo(Fid: "abc/de 200", FabricBrand: "维达莱", FabricMaterial: "90%羊毛 10%羊绒", FabricPrice: 5000, FabricImage: "tie2", FabricScale: 20),
            FabricInfo(Fid: "abc/de 400", FabricBrand: "维达莱", FabricMaterial: "100%棉", FabricPrice: 5000, FabricImage: "tie3", FabricScale: 12),
            FabricInfo(Fid: "abc/de 400", FabricBrand: "维达莱", FabricMaterial: "100%丝", FabricPrice: 5000, FabricImage: "tie4", FabricScale: 12),
            FabricInfo(Fid: "abc/de 400", FabricBrand: "维达莱", FabricMaterial: "100%棉", FabricPrice: 5000, FabricImage: "tie5", FabricScale: 12),
            FabricInfo(Fid: "abc/de 400", FabricBrand: "维达莱", FabricMaterial: "100%棉", FabricPrice: 5000, FabricImage: "tie6", FabricScale: 12),
        ],
    ]

    
    override func viewDidLoad() {
        super.viewDidLoad()
        fabricMap["kuzi"] = fabricMap["xizhuang"]
        reloadTable()
        tableView.delegate = self
    }

    func reloadTable() {
        if let s = receiveData {
            log.debug("receive data: \(s.dressType)")
            contents = fabricMap[s.selectPart!]
        }else {
            log.debug("not receive data!")
        }
        tableView.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if let c = contents {
            return c.count
        }else {
            return 0
        }
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("FabricCell", forIndexPath: indexPath) as! FabricDetailCell
        if let c = contents {
            let fabricInfo = c[indexPath.row]
            cell.setInfo(fabricInfo)
            self.tableView.rowHeight = 160
            return cell
        } else {
            return FabricDetailCell()
        }
    }

    // MARK: - Navigation
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cellRow = indexPath
        let selectedCell = self.tableView.cellForRowAtIndexPath(cellRow) as! FabricDetailCell
        receiveData!.selectFabric = selectedCell.fabricImage.image
        receiveData!.fabricScale = contents![cellRow.row].FabricScale
        log.debug("select row \(cellRow.row): \(receiveData!.debugDescription)")
        
        if let parent = parentViewController as? ContainerViewController {
            let scnViewCtrl = parent.scnViewController
            scnViewCtrl.afterSelectFabric(receiveData!)
        }
        
        let orientation = UIApplication.sharedApplication().statusBarOrientation
        if orientation == .Portrait || orientation == .PortraitUpsideDown{
            if let parent = parentViewController as? ContainerViewController {
                parent.hideFabric()
            }
        }
    }
}
